package com.example.manage.controller;

import com.example.manage.bean.ClientNewsBean;
import com.example.manage.bean.ResultBean;
import com.example.manage.mapper.ClientBannerMapper;
import com.example.manage.mapper.ClientNewsMapper;
import com.example.manage.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/clientNews")
public class ClientNewsController {
    @Autowired
    ClientNewsMapper clientNewsMapper;

    @RequestMapping("/addNews")
    @ResponseBody
    public ResultBean addNews(@RequestBody ClientNewsBean newsBean) {
        newsBean.setTime(System.currentTimeMillis());
        int count = clientNewsMapper.addNews(newsBean);
        return ResultUtil.getResultBean(count, "添加失败");
    }

    @RequestMapping("/updateNews")
    @ResponseBody
    public ResultBean updateNews(@RequestBody ClientNewsBean newsBean) {
        newsBean.setTime(System.currentTimeMillis());
        int count = clientNewsMapper.updateNews(newsBean);
        return ResultUtil.getResultBean(count, "修改失败");
    }


    @RequestMapping("/deleteNews")
    @ResponseBody
    public ResultBean deleteNews(@RequestBody Map<String, Long> params) {
        int count = clientNewsMapper.deleteNews(params.get("id"));
        return ResultUtil.getResultBean(count, "删除失败");
    }


    @RequestMapping("/getNews")
    @ResponseBody
    public ResultBean getNews() {
        List<ClientNewsBean> devices = clientNewsMapper.getNews();
        return ResultUtil.getSuccessBean(devices);
    }


    @RequestMapping("/getNewsById")
    @ResponseBody
    public ResultBean getNewsById(@RequestParam("id") Long id) {
        ClientNewsBean device = clientNewsMapper.getNewsById(id);
        return ResultUtil.getSuccessBean(device);
    }
}
